package edu.eas.admin.controller;

import edu.eas.admin.service.RoleService;
import edu.eas.common.api.CommonPage;
import edu.eas.common.api.CommonResult;
import edu.eas.common.dto.RoleParam;
import edu.eas.common.pojo.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cql
 */
@RestController
@Api(tags = "RoleController", description = "角色管理")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "新增角色")
    @PostMapping
    public CommonResult create(@Validated @RequestBody RoleParam role) {
        // TODO 先判断存不存在相同的字典数据
        int count = roleService.create(role);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }

    }


    @ApiOperation(value = "更新角色信息")
    @PutMapping("{roleId}")
    public CommonResult update(@PathVariable("roleId") Long roleId, @Validated @RequestBody RoleParam role) {
        int count = roleService.update(role);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量删除角色信息")
    @DeleteMapping("/{roleIds}")
    public CommonResult delete(@PathVariable Long[] roleIds) {
        int count = roleService.delete(roleIds);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "查询角色信息")
    @GetMapping("{roleId}")
    public CommonResult<Role> getInfo(@PathVariable("roleId") Long roleId) {
        Role role = roleService.selectRoleById(roleId);
        return CommonResult.success(role);
    }


    @ApiOperation(value = "分页查询字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = false),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false),
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = false),
            @ApiImplicitParam(name = "roleKey", value = "权限字符串", required = false),
            @ApiImplicitParam(name = "status", value = "启用状态：0:禁用; 1:启用", required = false),
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<Role>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "roleName", defaultValue = "") String roleName,
                                               @RequestParam(value = "roleKey", defaultValue = "") String roleKey,
                                               @RequestParam(value = "status", defaultValue = "-1") Integer status) {
        List<Role> menuList = roleService.list(pageSize, pageNum, roleName, roleKey, status);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

}
